﻿Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
            modMAIN.mySettings.SaveSettings()
            For xint As Int16 = 1 To Clients.Count - 1
                Clients(xint).GuiController.HideIcon()
            Next
            LogWindow.bAllowClose = True
        End Sub

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            'Dim MyEOC As New clsEOC("Marvin_The_Martian", "11314")
            If e.CommandLine.Count > 0 Then
                For Each Argument As String In e.CommandLine
                    If Argument.Contains("-createxml") Then
                        If Argument.Contains("-createxml:") Then
                            Dim nlocation As String = Argument.Replace("-createxml:", "").Trim(" ")
                            If nlocation.Contains("http://") Then
                                'Assume web location
                                If Not Cfg.CfgManager.SetupManager.LoadWebXML(nlocation) Then
                                    MsgBox("Could not use specified xml file")
                                    End
                                End If
                            Else
                                'Assume local file
                                If Not Cfg.CfgManager.SetupManager.LoadLocalXML(nlocation) Then
                                    MsgBox("Could not use specified xml file")
                                    End
                                End If
                            End If
                        Else
                            If Not Cfg.CfgManager.SetupManager.LoadWebXML(Xlocation) Then
                                MsgBox("Could not load web based xml template")
                                End
                            End If
                        End If
                        Application.MainForm = formXML
                    ElseIf Argument.Contains("-tray") Or Argument.Contains("-Tray") Then
                        Application.MainForm = fTray
                        EntryPoint(True)
                    End If
                Next
            Else
                Application.MainForm = frmHWINFO
            End If
        End Sub

        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            e.BringToForeground = True
        End Sub


        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            MsgBox("Unhandeld error!" & vbNewLine & vbNewLine & e.Exception.Message & vbNewLine & e.Exception.InnerException.ToString)
            e.ExitApplication = True
        End Sub
    End Class

End Namespace

